﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using eTMS.Commons;
using eTMS.Controllers;
using eTMS.Forms;

namespace eTMS.UserControls
{
    public partial class SinhVienList : eTMS.Controls.UserControlBase
    {
        public SinhVienList()
        {
            InitializeComponent();
        }
        #region Variables
        private bool _AllowDragSinhVien = false;
        private string _RedCheckColumn = "";
        public string RedCheckColumn
        {
            set
            {
                _RedCheckColumn = value;
            }
        }
        public bool AllowDragSinhVien
        {
            set
            {
                _AllowDragSinhVien = value;
            }
            get
            {
                return _AllowDragSinhVien;
            }
        }
        private SinhVienController controller;
        public void SetController(SinhVienController _controller)
        {
            controller = _controller;
        }
        public void SetDataSource(DataTable tblData)
        {
            gridData.AutoGenerateColumns = false;
            gridData.DataSource = tblData;
        }
        #endregion
        public DataGridViewColumn GetColumn(string ColumnName)
        {
            
            return gridData.Columns[ColumnName];
        }
        public void AddControlToBottomPanel(Control[] controls, bool IsInCenter)
        {
            int width = 0;
            if (cbkAll.Visible)
                width = cbkAll.Right + 2;
            foreach (Control ctrl in controls)
            {
                panel1.Controls.Add(ctrl);
                ctrl.Top = txtSoSV.Top;
                ctrl.Left = width;
                ctrl.Anchor = AnchorStyles.Top | AnchorStyles.Left;
                width += ctrl.Width + 2;
            }
            if (IsInCenter)
            {
                if (width > 0)
                    width = width - 2;
                int pos = (this.panel1.Width - width) / 2;
                foreach (Control ctrl in controls)
                {
                    ctrl.Left = pos;
                    ctrl.Anchor = AnchorStyles.Top;
                    pos += ctrl.Width + 2;
                }
            }
        }
        public void AddControlToBottomPanel(Control[] controls)
        {
            AddControlToBottomPanel(controls, true);
        }
        public bool IsCheckable
        {
            get
            {
                return cbkAll.Visible;
            }
            set
            {
                cbkAll.Visible = value;
                ColumnCheck.Visible = cbkAll.Visible;
            }
        }
        public void UpdateToScreen()
        {
            gridData.AutoGenerateColumns = false;
            gridData.DataSource = controller.Table;
            gridData.AutoResizeColumns();
        }
        public event EventHandler OnErrorValidate;
        public event EventHandler OnSelectionChanged;
        public void UpdateObjectToModel()
        {
            if (gridData.SelectedRows.Count > 0)
            {
                controller.SinhVienObject = controller.MapDataRowToObject(((DataRowView)gridData.SelectedRows[0].DataBoundItem).Row);
            }
        }
        private void LoadLanguage()
        {
            this.ColumnSTT.HeaderText = Constants.APP_INDEX;
            ColumnMaSinhVien.HeaderText = SinhVienStringMapping.MaSinhVien;
            ColumnTenSinhVien.HeaderText = SinhVienStringMapping.TenSinhVien;
            ColumnDiaChi.HeaderText = SinhVienStringMapping.DiaChi;
            ColumnEmail.HeaderText = SinhVienStringMapping.Email;
            ColumnSoCMND.HeaderText = SinhVienStringMapping.SoCMND;
            ColumnDienThoai.HeaderText = SinhVienStringMapping.DienThoai;
        }
        private void SinhVienList_Load(object sender, EventArgs e)
        {
            LoadLanguage();
        }
        public DataGridViewTextBoxColumn AddColumn(string columnName, string columnText, string dataPropertyName, int columnIndex, bool IsReadOnly)
        {
            DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
            col.Name = columnName;
            col.HeaderText = columnText;
            col.DataPropertyName = dataPropertyName;
            col.ReadOnly = IsReadOnly;
            gridData.Columns.Insert(columnIndex, col);
            return col;
        }
        public DataGridViewTextBoxColumn AddColumn(string columnName, string columnText, string dataPropertyName, int columnIndex)
        {
            return AddColumn(columnName, columnText, dataPropertyName, columnIndex, true);
        }
        public void AddColumn(DataGridViewColumn col, int index)
        {
            gridData.Columns.Insert(index, col);
        }
        public DataGridViewRowCollection Rows
        {
            get
            {
                return gridData.Rows;
            }
        }
        public List<DataRow> GetSelectedSinhVienRows()
        {
            List<DataRow> list = new List<DataRow>();
            List<int> lsIndexs = new List<int>();
            foreach (DataGridViewRow row in gridData.SelectedRows)
            {
                lsIndexs.Add(row.Index);
            }
            foreach (DataGridViewCell cell in gridData.SelectedCells)
            {
                if (!lsIndexs.Contains(cell.RowIndex))
                    lsIndexs.Add(cell.RowIndex);
            }
            lsIndexs.Sort();
            foreach (int indexRow in lsIndexs)
            {
                list.Add(((DataRowView)gridData.Rows[indexRow].DataBoundItem).Row);
            }
            return list;
        }
        public DataRow GetSelectedSinhVienRow()
        {
            if (gridData.SelectedRows.Count > 0)
                return ((DataRowView)gridData.SelectedRows[0].DataBoundItem).Row;
            return null;
        }
        public void RefreshList()
        {
            gridData.Update();
        }
        private void gridData_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            if (_RedCheckColumn.Length == 0) return;
            DataRow dataRow = ((DataRowView)gridData.Rows[e.RowIndex].DataBoundItem).Row;
            if (!dataRow.Table.Columns.Contains(_RedCheckColumn)) return;
            if (!dataRow.IsNull(_RedCheckColumn))
            {
                if (Convert.ToBoolean(dataRow[_RedCheckColumn]))
                {
                    gridData.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Red;
                }
                else
                {
                    gridData.Rows[e.RowIndex].DefaultCellStyle.ForeColor = gridData.DefaultCellStyle.ForeColor;
                }
            }
            else
            {
                gridData.Rows[e.RowIndex].DefaultCellStyle.ForeColor = gridData.DefaultCellStyle.ForeColor;
            }
        }
        private Rectangle dragBoxFromMouseDown;
        private int rowIndexFromMouseDown;
        private int rowIndexOfItemUnderMouseToDrop;
        private void gridData_MouseMove(object sender, MouseEventArgs e)
        {
            if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
            {
                // If the mouse moves outside the rectangle, start the drag.
                if (dragBoxFromMouseDown != Rectangle.Empty &&
                !dragBoxFromMouseDown.Contains(e.X, e.Y))
                {
                    // Proceed with the drag and drop, passing in the list item.                    
                    DragDropEffects dropEffect = gridData.DoDragDrop(
                    GetSelectedSinhVienRows(),
                    DragDropEffects.Move);
                }
            }
        }

        private void gridData_MouseDown(object sender, MouseEventArgs e)
        {
            // Get the index of the item the mouse is below.
            rowIndexFromMouseDown = gridData.HitTest(e.X, e.Y).RowIndex;
            if (rowIndexFromMouseDown != -1)
            {
                // Remember the point where the mouse down occurred. 
                // The DragSize indicates the size that the mouse can move 
                // before a drag event should be started.                
                Size dragSize = SystemInformation.DragSize;
                // Create a rectangle using the DragSize, with the mouse position being
                // at the center of the rectangle.
                dragBoxFromMouseDown = new Rectangle(new Point(e.X - (dragSize.Width / 2),
                e.Y - (dragSize.Height / 2)),
                dragSize);
            }
            else
                // Reset the rectangle if the mouse is not over an item in the ListBox.
                dragBoxFromMouseDown = Rectangle.Empty;
        }

        private void gridData_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            if (OnErrorValidate != null)
            {
                OnErrorValidate(sender, e);
            }
        }

        private void gridData_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            txtSoSV.Text = gridData.RowCount.ToString();
        }

        private void gridData_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {
            txtSoSV.Text = gridData.RowCount.ToString();
        }

        private void cbkAll_CheckedChanged(object sender, EventArgs e)
        {
            Commons.Common.FillTableColumnValue(controller.Table, "CHECKED", cbkAll.Checked);
        }
        public List<DataRow> GetCheckedSinhVienRows()
        {
            List<DataRow> list = new List<DataRow>();
            foreach (DataRowView view in this.controller.Table.DefaultView)
            {
                if ((bool)view["CHECKED"])
                {
                    list.Add(view.Row);
                }
            }
            return list;
        }

        private void gridData_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                if (OnSelectionChanged != null)
                {
                    OnSelectionChanged(sender, new EventArgs());
                }
            }
        }
    }
}
